<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/11/22
  Time: 9:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/static/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js"
            charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        span {
            color: red;
        }
    </style>

</head>

<body class="form-wrap">

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">购买产品</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form id="f1" class="layui-form" action="${pageContext.request.contextPath}/order/addOrder.do"
                  lay-filter="component-form-group">
                <input type="hidden" name="productId" value="${productVo.productId}">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">商品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" id="un" name="productName" autocomplete="off"
                                   class="layui-input" value="${productVo.productName}" readonly>
                        </div>
                        <span id="uns"></span>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">商品简介</label>
                        <div class="layui-input-inline">
                            <input type="text" name="productSize" autocomplete="off"
                                   class="layui-input" value="${productVo.productSize}" readonly>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">商品价格</label>
                        <div class="layui-input-inline">
                            <input type="text" name="productMoney" autocomplete="off"
                                   class="layui-input" value="${productVo.productMoney}" readonly>
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">购买数量</label>
                        <div class="layui-input-inline">
                            <input type="text" id="productNum" name="productNum" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">收货地址</label>
                        <div class="layui-input-inline">
                            <input type="text" name="orderAddress" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">收货号码</label>
                        <div class="layui-input-inline">
                            <input type="text" id="orderUphone" name="orderUphone" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button onclick="sub()" class="layui-btn" lay-submit=""
                                    lay-filter="component-form-demo1"
                                    type="button">提交订单
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>


<script>

    function sub() {
        var productNum = $("#productNum").val();
        var orderUphone = $("#orderUphone").val();

        if (productNum == null || productNum.trim() == "") {
            alert("请输入购买数量");
            return;
        } else if (productNum <= 0) {
            alert("请至少购买一个")
            return;
        }
        if (orderUphone.length != 11) {
            alert("请输入11位的手机号码")
            return;
        }
        $.ajax({
            type: "get",
            url: "${pageContext.request.contextPath}/sale/findWarehouseProduct.do?productId=" + ${productVo.productId},
        }).then(res => {
            if (res == null || res.trim() == "") {
                alert("库存不足")
            } else {
                var data = eval("(" + res + ")")
                for (var i = 0; i < data.length; i++) {
                    if (productNum <= data[i].warehouseProductQuantity) {
                        $.ajax({
                            type: "get",
                            url: "${pageContext.request.contextPath}/sale/update.do?productId=" + ${productVo.productId} +"&buyNum=" + productNum + "&Num=" + data[i].warehouseProductQuantity + "&warehouseId=" + data[i].warehouseId
                        }).then(res => {
                            alert(res);
                        })
                        $("#f1").submit();
                    }
                }
            }

        })
    }

</script>


<style id="LAY_layadmin_theme">.layui-side-menu, .layadmin-pagetabs .layui-tab-title li:after, .layadmin-pagetabs .layui-tab-title li.layui-this:after, .layui-layer-admin .layui-layer-title, .layadmin-side-shrink .layui-side-menu .layui-nav > .layui-nav-item > .layui-nav-child {
    background-color: #20222A !important;
}

.layui-nav-tree .layui-this, .layui-nav-tree .layui-this > a, .layui-nav-tree .layui-nav-child dd.layui-this, .layui-nav-tree .layui-nav-child dd.layui-this a {
    background-color: #009688 !important;
}

.layui-layout-admin .layui-logo {
    background-color: #20222A !important;
}</style>
</body>


</html>
